Security News
New Python Packaging Proposal Aims to Solve Phantom Dependency Problem with SBOMs
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
@wordpress/rich-text
Advanced tools
This module contains helper functions to convert HTML or a DOM tree into a rich text value and back, and to modify the value with functions that are similar to String
methods, plus some additional ones for formatting.
Install the module
npm install @wordpress/rich-text
This package assumes that your code will run in an ES2015+ environment. If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using core-js or @babel/polyfill will add support for these methods. Learn more about it in Babel docs.
# applyFormat
Apply a format object to a Rich Text value from the given startIndex
to the
given endIndex
. Indices are retrieved from the selection if none are
provided.
Parameters
RichTextValue
: Value to modify.RichTextFormat
: Format to apply.[number]
: Start index.[number]
: End index.Returns
RichTextValue
: A new value with the format applied.# concat
Combine all Rich Text values into one. This is similar to
String.prototype.concat
.
Parameters
...RichTextValue
: Objects to combine.Returns
RichTextValue
: A new value combining all given records.# create
Create a RichText value from an Element
tree (DOM), an HTML string or a
plain text string, with optionally a Range
object to set the selection. If
called without any input, an empty value will be created. If
multilineTag
is provided, any content of direct children whose type matches
multilineTag
will be separated by two newlines. The optional functions can
be used to filter out content.
A value will have the following shape, which you are strongly encouraged not to modify without the use of helper functions:
{
text: string,
formats: Array,
replacements: Array,
?start: number,
?end: number,
}
As you can see, text and formatting are separated. text
holds the text,
including any replacement characters for objects and lines. formats
,
objects
and lines
are all sparse arrays of the same length as text
. It
holds information about the formatting at the relevant text indices. Finally
start
and end
state which text indices are selected. They are only
provided if a Range
was given.
Parameters
[Object]
: Optional named arguments.[Element]
: Element to create value from.[string]
: Text to create value from.[string]
: HTML to create value from.[Range]
: Range to create value from.[string]
: Multiline tag if the structure is multiline.[Array]
: Tags where lines can be found if nesting is possible.[boolean]
: Whether or not to collapse white space characters.[boolean]
:Returns
RichTextValue
: A rich text value.# getActiveFormat
Gets the format object by type at the start of the selection. This can be used to get e.g. the URL of a link format at the current selection, but also to check if a format is active at the selection. Returns undefined if there is no format at the selection.
Parameters
RichTextValue
: Value to inspect.string
: Format type to look for.Returns
RichTextFormat|undefined
: Active format object of the specified type, or undefined.# getActiveObject
Gets the active object, if there is any.
Parameters
RichTextValue
: Value to inspect.Returns
RichTextFormat|void
: Active object, or undefined.# getTextContent
Get the textual content of a Rich Text value. This is similar to
Element.textContent
.
Parameters
RichTextValue
: Value to use.Returns
string
: The text content.# insert
Insert a Rich Text value, an HTML string, or a plain text string, into a
Rich Text value at the given startIndex
. Any content between startIndex
and endIndex
will be removed. Indices are retrieved from the selection if
none are provided.
Parameters
RichTextValue
: Value to modify.RichTextValue|string
: Value to insert.[number]
: Start index.[number]
: End index.Returns
RichTextValue
: A new value with the value inserted.# insertObject
Insert a format as an object into a Rich Text value at the given
startIndex
. Any content between startIndex
and endIndex
will be
removed. Indices are retrieved from the selection if none are provided.
Parameters
RichTextValue
: Value to modify.RichTextFormat
: Format to insert as object.[number]
: Start index.[number]
: End index.Returns
RichTextValue
: A new value with the object inserted.# isCollapsed
Check if the selection of a Rich Text value is collapsed or not. Collapsed
means that no characters are selected, but there is a caret present. If there
is no selection, undefined
will be returned. This is similar to
window.getSelection().isCollapsed()
.
Parameters
RichTextValue
: The rich text value to check.Returns
boolean|undefined
: True if the selection is collapsed, false if not, undefined if there is no selection.# isEmpty
Check if a Rich Text value is Empty, meaning it contains no text or any objects (such as images).
Parameters
RichTextValue
: Value to use.Returns
boolean
: True if the value is empty, false if not.# join
Combine an array of Rich Text values into one, optionally separated by
separator
, which can be a Rich Text value, HTML string, or plain text
string. This is similar to Array.prototype.join
.
Parameters
Array<RichTextValue>
: An array of values to join.[string|RichTextValue]
: Separator string or value.Returns
RichTextValue
: A new combined value.# registerFormatType
Registers a new format provided a unique name and an object defining its behavior.
Parameters
string
: Format name.WPFormat
: Format settings.Returns
WPFormat|undefined
: The format, if it has been successfully registered; otherwise undefined
.# remove
Remove content from a Rich Text value between the given startIndex
and
endIndex
. Indices are retrieved from the selection if none are provided.
Parameters
RichTextValue
: Value to modify.[number]
: Start index.[number]
: End index.Returns
RichTextValue
: A new value with the content removed.# removeFormat
Remove any format object from a Rich Text value by type from the given
startIndex
to the given endIndex
. Indices are retrieved from the
selection if none are provided.
Parameters
RichTextValue
: Value to modify.string
: Format type to remove.[number]
: Start index.[number]
: End index.Returns
RichTextValue
: A new value with the format applied.# replace
Search a Rich Text value and replace the match(es) with replacement
. This
is similar to String.prototype.replace
.
Parameters
RichTextValue
: The value to modify.RegExp|string
: A RegExp object or literal. Can also be a string. It is treated as a verbatim string and is not interpreted as a regular expression. Only the first occurrence will be replaced.Function|string
: The match or matches are replaced with the specified or the value returned by the specified function.Returns
RichTextValue
: A new value with replacements applied.# slice
Slice a Rich Text value from startIndex
to endIndex
. Indices are
retrieved from the selection if none are provided. This is similar to
String.prototype.slice
.
Parameters
RichTextValue
: Value to modify.[number]
: Start index.[number]
: End index.Returns
RichTextValue
: A new extracted value.# split
Split a Rich Text value in two at the given startIndex
and endIndex
, or
split at the given separator. This is similar to String.prototype.split
.
Indices are retrieved from the selection if none are provided.
Parameters
RichTextValue
:[number|string]
: Start index, or string at which to split.Returns
Array<RichTextValue>
: An array of new values.# store
Store definition for the rich-text namespace.
Related
Type
Object
# toggleFormat
Toggles a format object to a Rich Text value at the current selection.
Parameters
RichTextValue
: Value to modify.RichTextFormat
: Format to apply or remove.Returns
RichTextValue
: A new value with the format applied or removed.# toHTMLString
Create an HTML string from a Rich Text value. If a multilineTag
is
provided, text separated by a line separator will be wrapped in it.
Parameters
Object
: Named argements.RichTextValue
: Rich text value.[string]
: Multiline tag.[boolean]
: Whether or not to use newline characters for line breaks.Returns
string
: HTML string.# unregisterFormatType
Unregisters a format.
Parameters
string
: Format name.Returns
RichTextFormatType|undefined
: The previous format value, if it has been successfully unregistered; otherwise undefined
.# useAnchorRef
This hook, to be used in a format type's Edit component, returns the active
element that is formatted, or the selection range if no format is active.
The returned value is meant to be used for positioning UI, e.g. by passing it
to the Popover
component.
Parameters
Object
: Named parameters.RefObject<HTMLElement>
: React ref of the element containing the editable content.RichTextValue
: Value to check for selection.RichTextFormatType
: The format type's settings.Returns
Element|Range
: The active element or selection range.FAQs
Rich text value and manipulation API.
The npm package @wordpress/rich-text receives a total of 56,450 weekly downloads. As such, @wordpress/rich-text popularity was classified as popular.
We found that @wordpress/rich-text demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PEP 770 proposes adding SBOM support to Python packages to improve transparency and catch hidden non-Python dependencies that security tools often miss.
Security News
Socket CEO Feross Aboukhadijeh discusses open source security challenges, including zero-day attacks and supply chain risks, on the Cyber Security Council podcast.
Security News
Research
Socket researchers uncover how threat actors weaponize Out-of-Band Application Security Testing (OAST) techniques across the npm, PyPI, and RubyGems ecosystems to exfiltrate sensitive data.